﻿//using FrameServer.Net;
//using System.Linq;
//using System.Collections.Concurrent;
//using System.Collections.Generic;

//namespace FrameServer
//{
//    class MatchMgr : Singleton<MatchMgr>
//    {
//        //一个游戏模式下的玩家匹配列表, kv: mode - dict(uid-entity)
//        private readonly Dictionary<int, Dictionary<int, MatchPlayerEntity>> dictClientsInQ = new Dictionary<int, Dictionary<int, MatchPlayerEntity>>();
        
//        //每个游戏模式对应的开局数量配置，达到该数量即可开局, kv: mode - num
//        private readonly Dictionary<int, int> numConfig = new Dictionary<int, int>();

//        private static readonly object _lock = new object();

//        protected override void AutoInit()
//        {
//            dictClientsInQ[0] = new Dictionary<int, MatchPlayerEntity>();//模式0 下的所有匹配中玩家列表
//            dictClientsInQ[1] = new Dictionary<int, MatchPlayerEntity>();
//            dictClientsInQ[2] = new Dictionary<int, MatchPlayerEntity>();

//            numConfig[0] = 1;//模式0 只要有1个玩家即可开始对局
//            numConfig[1] = 1;
//            numConfig[2] = 2;//模式2 需要有2个玩家方可开始对局
//        }


//        public void RcvMatchRequest(Match match, TCPClientPeer peer) {
//            int mode = match.Mode;
//            int role = match.Role;
//            peer.role = role;

//            peer.SendTcpMsg(S2CCode.MatchResponse, new Empty());

//            AddToMatchQueue(mode, peer.uid, role);
//        }
//        public void RcvCancelRequest(MatchCancel matchCancel, TCPClientPeer peer)
//        {
//            int mode = matchCancel.Mode;
//            RemoveFromMatchQueue(mode, peer.uid);
//            peer.SendTcpMsg(S2CCode.MatchCancelResponse, new Empty());
//        }
//        private void AddToMatchQueue(int mode, int uid, int role)
//        {
//            lock (_lock)
//            {
//                if (dictClientsInQ[mode].ContainsKey(uid))
//                    return;

//                MatchPlayerEntity entity = new MatchPlayerEntity { uid = uid, role = role };
//                dictClientsInQ[mode].Add(uid, entity);
//                if (dictClientsInQ[mode].Count >= numConfig[mode])
//                {
//                    //已经确认的要开局的玩家列表
//                    List<MatchPlayerEntity> confirmedClients = new List<MatchPlayerEntity>();
//                    List<int> uidList = dictClientsInQ[mode].Keys.ToList();
//                    for (int i = 0; i < numConfig[mode]; i++)
//                    {
//                        int _uid = uidList[i];
//                        if (TCPServer.Instance.GetTcpPeer(_uid) == null || !TCPServer.Instance.GetTcpPeer(_uid).IsConnected())
//                        {
//                            //说明此玩家掉线了，把他移出匹配队列，清空确认列表，结束这次匹配。
//                            dictClientsInQ[mode].Remove(_uid);
//                            confirmedClients.Clear();
//                            return;
//                        }
//                        confirmedClients.Add(dictClientsInQ[mode][_uid]);
//                    }

//                    foreach (MatchPlayerEntity item in confirmedClients)
//                        dictClientsInQ[mode].Remove(item.uid);

//                    BattleMgr.Instance.BeginCreateBattle(mode, confirmedClients);
//                }
//            }

//        }

//        private void RemoveFromMatchQueue(int mode, int uid)
//        {
//            lock (_lock)
//            {
//                dictClientsInQ[mode].Remove(uid);
//            }
//        }
//        public void RemoveFromMatchQueue(int uid)
//        {
//            lock (_lock)
//            {
//                foreach (Dictionary<int, MatchPlayerEntity> item in dictClientsInQ.Values)
//                    item.Remove(uid);
//            }
//        }
//    }

//    public class MatchPlayerEntity
//    {
//        public int uid;
//        public int role;
//    }
//}
